package com.campusmarket.service;

import com.campusmarket.common.base.BaseService;
import com.campusmarket.dto.CommodityDto;
import com.campusmarket.dto.LayuiDataGridResult;
import com.campusmarket.mapper.UserMapper;
import com.campusmarket.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.List;

@Service
public class UserService extends BaseService<User>{

    @Autowired
    private UserMapper userMapper;

    public boolean register(User user) {
        try{
            log.info("========开始注册========");
            user.setCoin(0F);
            userMapper.insert(user);
        }catch (Exception e){
            //注册失败
            return false;
        }
        //注册成功
        return true;

    }


    public boolean login(String username, String password){
            log.info("正在登陆");
            if(userMapper.selectByAccount(username).getPassword().equals(password))
                return true;
            else return false;
    }

    public boolean user_edit(User user){
        try{
            log.info("修改个人资料");
            userMapper.updateByPrimaryKey(user);
        }catch (Exception e){
            //修改失败
            return false;
        }
        //修改成功
        return true;
    }

    public LayuiDataGridResult selectListByPage(int page, int limit, String key, User user) {
        LayuiDataGridResult result=new LayuiDataGridResult();
        Integer count=userMapper.selectCount();
        List<User> list;
        if(user!=null){
            list=userMapper.selectList(page-1,limit,key);
        }else{
            list=userMapper.selectList(page-1,limit,key);
        }
        result.setCount((long)count);
        result.setData(list);
        return result;
    }

    public User selectByAccount(String username) {
        return userMapper.selectByAccount(username);
    }


//    public void test(){
//        userMapper.selectAll();
//    }
}
